package cn.zy.management;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GUI extends JFrame {
	
	public GUI() {
		JPanel mainPanel = new JPanel(new GridLayout(10, 2));
		
		JLabel jlServer = new JLabel("Server: ");
		final JTextField jtfServer = new JTextField("localhost");
		
		JLabel jlPort = new JLabel("Port: ");
		final JTextField jtfPort = new JTextField("1433");
		
		JLabel jlDatabase = new JLabel("Database: ");
		final JTextField jtfDatabase = new JTextField();
		
		JLabel jlUsername = new JLabel("Username: ");
		final JTextField jtfUsername = new JTextField();
		
		JLabel jlPassword = new JLabel("Password: ");
		final JTextField jtfPassword = new JTextField();
		
		JLabel jlDirectory = new JLabel("Output Dir: ");
		JTextField jtfDirectory = new JTextField("D:\\Result.jpg");
		jtfDirectory.setEnabled(false);
		
		JLabel jlTableName = new JLabel("Table Name: ");
		final JTextField jtfTableName = new JTextField();

		JLabel jlColumn1 = new JLabel("Column 1: ");
		final JTextField jtfColumn1 = new JTextField();
		
		JLabel jlColumn2 = new JLabel("Column 2: ");
		final JTextField jtfColumn2 = new JTextField();
		
		JButton jbGenerate = new JButton("Generate");
		final JButton jbConnect = new JButton("Connect");
		
		jbGenerate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (dbUtil == null) {
					JOptionPane.showMessageDialog(null, "Please connect database first", "Error", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				if (generatePicture())
					JOptionPane.showMessageDialog(null, "Picture generate successfully", "Information", JOptionPane.PLAIN_MESSAGE);
				else
					JOptionPane.showMessageDialog(null, "Picture generate failed", "Error", JOptionPane.ERROR_MESSAGE);
				
				jbConnect.setEnabled(true);
			}
		});
		
		jbConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (jtfDatabase.getText().equals("")
							|| jtfUsername.getText().equals("")
							//|| jtfPassword.getText().equals("")
							|| jtfTableName.getText().equals("")
							|| jtfColumn1.getText().equals("")
							|| jtfColumn2.getText().equals("")) {
						JOptionPane.showMessageDialog(null, "No empty field permissed", "Error", JOptionPane.ERROR_MESSAGE);
						return;
					}
					
					dbUtil = new DBUtil(jtfServer.getText(), jtfPort.getText(), jtfDatabase.getText(), jtfUsername.getText(), jtfPassword.getText(), jtfTableName.getText(), jtfColumn1.getText(), jtfColumn2.getText());
					boolean successful = dbUtil.testConnection();
					
					if (successful) {
						JOptionPane.showMessageDialog(null, "Connect Database Successfully", "Information", JOptionPane.PLAIN_MESSAGE);
						jbConnect.setEnabled(false);
					}
					else {
						JOptionPane.showMessageDialog(null, "Connect Database failed", "Error", JOptionPane.ERROR_MESSAGE);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		
		mainPanel.add(jlServer);
		mainPanel.add(jtfServer);
		mainPanel.add(jlPort);
		mainPanel.add(jtfPort);
		mainPanel.add(jlDatabase);
		mainPanel.add(jtfDatabase);
		mainPanel.add(jlUsername);
		mainPanel.add(jtfUsername);
		mainPanel.add(jlPassword);
		mainPanel.add(jtfPassword);
		mainPanel.add(jlDirectory);
		mainPanel.add(jtfDirectory);
		mainPanel.add(jlTableName);
		mainPanel.add(jtfTableName);
		mainPanel.add(jlColumn1);
		mainPanel.add(jtfColumn1);
		mainPanel.add(jlColumn2);
		mainPanel.add(jtfColumn2);
		mainPanel.add(jbConnect);
		mainPanel.add(jbGenerate);
		
		this.add(mainPanel, BorderLayout.CENTER);
		
		this.setTitle("Generator");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.pack();
		this.setResizable(false);
	}
	
	private boolean generatePicture() {
		StringPair pair = null;
		try {
			pair = dbUtil.getAllNameAndIP();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		int firstSetSize = pair.getFirstListSet().size();
		int secondSetSize = pair.getSecondListSet().size();
		PaintUtil paintUtil = new PaintUtil(firstSetSize > secondSetSize ? firstSetSize : secondSetSize);
		try {
			paintUtil.paintAll(pair);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String[] args) {
		new GUI();
	}
	
	private DBUtil dbUtil;
}
